<template>
  <el-dialog v-model="dialogFormVisible" title="编辑用户">
    <el-form :model="form">
      <el-row>
        <el-col :span="2"
          ><div class="grid-content ep-bg-purple-light"
        /></el-col>
        <el-col :span="22"> 当前的用户： {{ form.username }} </el-col>
      </el-row>

      <el-row>
        <el-col :span="2"
          ><div class="grid-content ep-bg-purple-light"
        /></el-col>
        <el-col :span="22"> 当前的角色： {{ form.role_name }}</el-col>
      </el-row>

      <el-row>
        <el-col :span="2"
          ><div class="grid-content ep-bg-purple-light"
        /></el-col>
        <el-col :span="22">
          分配新角色:
          <el-select v-model="id" class="m-2" size="large">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.roleName"
              :value="item.id"
            />
          </el-select>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="getFrom">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref, defineEmits } from "vue";
import { usersPower } from "@/api/Users";

import { useStore } from "vuex";
const store = useStore();
const id = ref("");
const options = reactive<any>([
  // {
  //   id: "管理员",
  //   roleName: "管理员",
  // },
]);
// 用户id
const userid = ref<number>();
// 判断是否清空option
const getOption = (useid: number) => {
  console.log(321123);
  userid.value = useid;
  console.log(userid.value, 11111111111111111111);
  options.length != 0 || options.push(...store.state.user.userID);
  console.log(store.state.user.userID);

  // options.push(...store.state.user.userID);
};
console.log(123456);

// 接收父组件方法并传入表单数据
const emit = defineEmits(["getPower"]);
//确认按钮
const getFrom = async () => {
  // console.log(store.state.user.userID, "分配角色信息----------");
  dialogFormVisible.value = false;
  console.log(form, "form表单", form.id);
  emit("getPower", num.value, form);
  let res = await usersPower(userid.value as number, { rid: form.id });
  console.log(res);
};

const dialogTableVisible = ref(false);
// 打开弹窗
const dialogFormVisible = ref<boolean>(false);

const formLabelWidth = "140px";

const form: {
  username: string;
  role_name: string;
  id: number | string;
} = reactive({
  username: "",
  role_name: "",
  id,
});
const num = ref(0);
const openDialog = (
  index: number,
  row: { username: string; role_name: string }
) => {
  console.log(row, index);
  dialogFormVisible.value = true;
  num.value = index;
  form.username = row.username;
  form.role_name = row.role_name;
};
// 将子组件的方法暴露出去
defineExpose({
  openDialog,
  getOption,
});
</script>
<style scoped>
.el-table .el-table__cell.is-right {
  text-align: left !important;
}
.el-button--text {
  margin-right: 15px;
}
.el-select {
  width: 300px;
}
.el-input {
  width: 300px;
}
.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>
